home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: strange segementtation fault w/ fprintf
- Date: 15 Feb 1996 20:59:54 GMT
- Organization: OpenVision
- Message-ID: <4g06sa$o20@spanky.pls.ov.com>
- References: <312215BE.41C6@di.epfl.ch>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 41C6@di.epfl.ch, Olivier Georg <ogeorg@di.epfl.ch> writes:
- >Hi,
- >
- >I have a very strange run-time error. I'm using fprintf
- >to write to a file, but I get a core dumped while everything
- >seems OK. Here's the code:
- >
- > main() {
- > ...
- > printf("variable to save data to:"); scanf("%s", &varname);
- > strcpy(filename, varname); strcat(filename, ".yyy");
- >
- >at this point filename has value, say, "b.yyy"
- >
- > fp = fopen(filename,"w");
- >
- >fp has value 0xfb52924, so it is not 0...
- >
- > writevector(fp, totalLength, toSave);
- > fclose(fp);
- > ...
- > }
- >
- >writevector is:
- >
- > int writevector(FILE *fp,
- > int len,
- > double vect[])
- > {
- > char str[64];
- > int i = 0;
- > for (i=0; i<len; i++) {
- > printf("vect[%d] = %f\n", i, vect[i]);
- > fprintf(fp, "aaa\n");
- > fprintf(fp, "%f\n", vect[i]);
- > }
- > }
- >
- >vect is a valid array, and the printf above prints the
- >correct value. I said earlier that fp had a correct
- >value. But when the first fprintf is reached, I get
- >a segmentation fault... using GDB, I did a "where" where
- >the seg. fault occured, and here is the result:
- >
- >(gdb) where
- >#0 0xfabc814 in __malloc () at malloc.c:303
- >#1 0xfabf1e4 in _malloc () at malloc.c:484
- >#2 0xfac6830 in _findbuf () at _findbuf.c:96
- >#3 0xfabca38 in _doprnt () at doprnt.c:1588
- >#4 0xfac7ffc in fprintf () at fprintf.c:45
- >#5 0x402c04 in writevector (fp=0xfb52914, len=128, vect=0x100022f0) at
- >mlutil.c:99
- >#6 0x40256c in main () at online.c:219
- >
- >Any idea of what that could be will be welcome...
- >(my project is due for next monday...)
- >
- >
- >Olivier
-
-
- I doubt that you have included your entire program above. The gdb
- trace indicated that you really crashed in malloc() which means that
- the heap has been corrupted by your program. There is a lot of
- information in the FAQ about malloc() crashes, and how they are
- caused. :-)
-
- Fletcher.Glenn@ov.com
-
-
-